package cn.tannn.tconfig.server.controller;

import cn.tannn.tconfig.server.controller.dto.ConfigsAdd;
import cn.tannn.tconfig.server.controller.dto.ConfigsPage;
import cn.tannn.tconfig.server.model.Configs;
import cn.tannn.tconfig.server.response.PageResultVO;
import cn.tannn.tconfig.server.service.ConfigsService;
import cn.tannn.tconfig.server.DistributedLocks;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户端注册上来的配置信息接口
 *
 * @author <a href="https://tannn.cn/">tan</a>
 * @date 2024/4/28 13:02
 */
@RestController
@RequestMapping("configs")
@RequiredArgsConstructor
public class ConfigController {

    private final ConfigsService configsService;
    private final DistributedLocks distributedLocks;

    /**
     * version
     */
    Map<String, Long> VERSIONS = new HashMap<>();

    /**
     * 查询配置
     * @return Configs of List
     */
    @GetMapping("/findAll")
    public List<Configs> findAll() {
        return configsService.findAll();
    }

    /**
     * 查询配置
     * @param app 应用
     * @param env 环境
     * @param ns namespace
     * @return Configs of List
     */
    @GetMapping("/list")
    public List<Configs> list(String app, String env, String ns) {
        return configsService.list(app, env, ns);
    }

    @RequestMapping("/update")
    public List<Configs> update(@RequestParam("app") String app,
                                @RequestParam("env") String env,
                                @RequestParam("ns") String ns,
                                @RequestBody Map<String, String> params) {
        params.forEach((k, v) -> insertOrUpdate(new Configs().create(app, env, ns, k, v)));
        VERSIONS.put(app + "-" + env + "-" + ns, System.currentTimeMillis());
        return configsService.list(app, env, ns);
    }



    /**
     * 获取配置信息的版本
     * @param app 应用
     * @param env 环境
     * @param ns namespace
     * @return version
     */
    @GetMapping("/version")
    public long version(String app, String env, String ns) {
        return VERSIONS.getOrDefault(app + "-" + env + "-" + ns, -1L);
    }


    /**
     * @return 锁状态 [true: 主 false: 从]
     */
    @GetMapping("/status")
    public boolean status() {
        return distributedLocks.getLocked().get();
    }



    @PostMapping("/page")
    public PageResultVO<Configs> page(@RequestBody ConfigsPage page){
        Page<Configs> pageData = configsService.page(page);
        return PageResultVO.success(pageData);
    }


    /**
     * 更新/新增 [where app,env,ns,pkey]
     * @param configs {@link Configs}
     */
    private void insertOrUpdate(Configs configs) {
        configsService.select(configs.getApp(), configs.getEnv(),
                configs.getNs(), configs.getPkey()).ifPresentOrElse(
                conf -> configsService.update(configs),
                () -> configsService.insert(new ConfigsAdd(configs)));
    }
}
